<?php

declare (strict_types=1);

namespace app\middleware;

/**
 * 全局跨域请求处理
 * Class CrossDomain
 * @package app\middleware
 */
class CrossDomain extends Base
{
    public function handle($request, \Closure $next)
    {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Max-Age: 1800');
        header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, OPTIONS, DELETE');
        header('Access-Control-Allow-Headers: *');
        if (strtoupper($request->method()) == "OPTIONS") {
            return \response('', 204, []);
        }

        return $next($request);
    }
}